<template>
  <div class="about">
    <h1>This is an about page</h1>
    <button @click="gaode1">普通高精度gps优先</button>
    <button @click="gaode2">普通高精度关闭gps优先</button>
    <button @click="gaode3">https</button>
    <button @click="gaode4">缓存定位</button>
    <button @click="gaode5">超时2s间隔2s</button>
    <button @click="gaode6">仅设备</button>
    <button @click="gaode7">仅网络</button>
    <button @click="gaode8">关闭地址信息提示</button>
    <button @click="gaode9">原GPS插件获取</button>
    <button @click="$router.push('./picker')">图形选择器</button>
    <button @click="$router.push('./h5photo')">网页摄像机</button>
    <button @click="gaode10">百度回调位置模式</button>
    <button @click="gaode12">百度关闭回调位置</button>
    <button @click="gaode11">百度gcj02算法</button>


    状态：{{status}}<br>
    经度：{{latitude}}<br>
    纬度：{{longitude}}<br>
    地图编码：{{locType}}<br>
    状态描述：{{locTypeDescription}}<br>
    精度：{{accuracy}}<br>
    地址：{{address}}<br>
    时间戳：{{time}}<br>
    卫星数：{{satellites}}<br>
    类型：{{type}}<br>
    ( 2 前次定位 4 缓存定位结果 5 Wifi定位结果 6 基站定位结果)
  </div>
</template>
<script>
  export default {
    name: "AllUse",
    data() {
      return {
        msg: 'hello vue',
        status:'',
        latitude:"",
        longitude:'',
        accuracy:'',
        address:'',
        time:'',
        type:'',
        satellites:'',
        options:{},
        locType:'',
        locTypeDescription:'',

      }
    },
    mounted: function () {


    },

    methods: {
      gaode1(){
       this.options = {GpsFirst:true,LocationMode:"Hight_Accuracy"};
       this.gaode();
      },
      gaode2(){
        this.options = {GpsFirst:false,LocationMode:"Hight_Accuracy"};
        this.gaode();
      },
      gaode3(){
        this.options = {AMapLocationProtocol:"HTTPS",GpsFirst:true};
        this.gaode();
      },
      gaode4(){
        this.options = {LocationCacheEnable:true,GpsFirst:true};
        this.gaode();
      },
      gaode5(){
        this.options = {HttpTimeOut:20000,Interval:2000,GpsFirst:true};
        this.gaode();
      },
      gaode6(){
        this.options = {LocationMode:"Device_Sensors"};
        this.gaode();
      },
      gaode7(){
        this.options = {LocationMode:"Battery_Saving"};
        this.gaode();
      },
      gaode8(){
        this.options = {NeedAddress:false};
        this.gaode();
      },
      gaode9(){
        this.gaodeGps();
      },
      gaode10(){
        //百度sdk正常模式，开启位置回调
        this.options = {};
        this.baiduGps();
      },
      gaode11(){
        //gcj02加密，OpenAutoNotifyMode:false,ScanSpan: 5000
        this.options = {CoorType:"gcj02",OpenAutoNotifyMode:false,ScanSpan: 5000};
        this.baiduGps();
      },
      gaode12(){
        //关闭位置回调，只定位一次间隔0，没有位置回调
        this.options = {ScanSpan:0,OpenAutoNotifyMode: false};
        this.baiduGps();
      },
      gaode(){
        setInterval(this.doGaode,10000);
      },
      gaodeGps(){
        setInterval(this.doGps,10000);
      },
      baiduGps(){
        setInterval(this.doBaiduGps,10000);
      },
      doGaode(){
        let that =this;
        function successFn(success){
          console.log(success);
          // {accuracy:550,adcode:"440305",address:"广东省深圳市南山区高新北六道10靠近兰光科技园",
          // backtime:1559899030603,bearing: 0, city:"深圳市",citycode: "0755",country: "中国", district: "南山区", type:4,
          // latitude:22.554149,longitude: 113.941515,poi:"兰光科技园", province:"广东省",satellites: 0,status: "定位成功", time: "1559899030136"}
          /* 精度 accuracy 角度 bearing 星数当前提供定位服务的卫星个数 satellites
          响应码 定位类型type:
          0 定位失败 请通过AMapLocation.getErrorCode()方法获取错误码，并参考错误码对照表进行问题排查。
          1 GPS定位结果 通过设备GPS定位模块返回的定位结果，精度较高，在10米－100米左右
          2 前次定位结果 网络定位请求低于1秒、或两次定位之间设备位置变化非常小时返回，设备位移通过传感器感知。
          4 缓存定位结果 返回一段时间前设备在同样的位置缓存下来的网络定位结果
          5 Wifi定位结果 属于网络定位，定位精度相对基站定位会更好，定位精度较高，在5米－200米之间。
          6 基站定位结果 纯粹依赖移动、联通、电信等移动网络定位，定位精度在500米-5000米之间。*/
          that.status = success.status;
          that.latitude = success.latitude;
          that.longitude = success.longitude;
          that.accuracy = success.accuracy;
          that.address = success.address;
          that.time = success.backtime;
          that.type = success.type;
          that.satellites = success.satellites;
        };
        function errorFn(error){
          console.log(error);
        };
        // 进行定位
        MyGaodePlugin.getCurrentPosition(successFn,errorFn,that.options);

      },
      doGps(){
        //下面是普通插件获取经/纬度/速度的方法
        let self = this ;
        function onSuccess(position) {
          console.log(position);
          self.longitude = position.coords.longitude;
          self.latitude = position.coords.latitude;
          self.$store.commit("updateLatitude",self.longitude);//更新VueX的经纬度
          self.$store.commit("updateLongitude",self.latitude);//更新VueX的经纬度

        }
        function error(error) {
          console.log(error);
          self.msg='code: '    + error.code    +
                  'message: ' + error.message + ' \n ';
        }
        //首先进入获取地理位置的方法maximumAge缓存，数值0费电，设置数值不超过该值会使用该缓存
        if (navigator.geolocation){
          console.log("if (navigator.geolocation)");
          navigator.geolocation.getCurrentPosition(onSuccess,error,{enableHighAccuracy:true});// maximumAge: 0, timeout: 3000,,{maximumAge: 3000, timeout: 5000,enableHighAccuracy: true }

        }else{
          console.log("该浏览器不支持获取地理位置。");
        }

      },
      doBaiduGps(){
        let me = this ;
        // 百度地图sdk进行定位
        let win = function(result){
          console.log(JSON.stringify(result));
          me.latitude = result.latitude;
          me.longitude = result.longitude;
          me.locType= result.locType;
          me.locTypeDescription = result.locTypeDescription;
          me.time = result.time;
        };
        let fail =  function (error) {
          console.log(error);
          me.status = error.message;
        };
        baidumap_location.getCurrentPosition(win,fail,me.options);
        /*baidumap_location.getCurrentPosition(win,fail);*/
      }

    },//method方法结束


  }
</script>

